Python for AI: A Crash Course

Nov 16 2024 · Python 3.12, JupyterLab 4.2.4

Lesson 03: Working with Python Libraries & Package Management

Virtual Environment Demo

Transcript

00:03In this exercise, you’ll create a virtual environment separate from your system’s main Python environment. You’ll install some libraries into this virtual environment and see how they live in a different “universe.”

00:16You’ll need a directory for this exercise’s virtual environment and Jupyter notebook files. Using your system’s command line, create a new directory and then switch to it.

00:25Create a new environment named my_env by entering the following on the command line:

python -m venv my_env

00:34This creates a virtual environment named my_env and a directory with the same name in the current directory. Use the ls command to get a listing of the current directory and confirm that the my_env directory was created.

00:47Enter pip list on the command line. You’ll see a list of all the Python packages currently installed on your system. If you’ve been following the lesson exercises, this list should be fairly long.

00:59Now that you’ve created a virtual environment, it’s time to activate it. Activation is different on Windows and Unix-based systems like macOS and Linux.

01:08On Windows, enter the following on the command line:

my_env\Scripts\activate

01:11On macOS and Linux, use this command instead:

source my_env/bin/activate

01:14After a moment or two, the activation process will complete, and you’ll see that your command-line prompts are preceded by (my_env) on Windows and my_env on macOS and Linux. You’re now in the my_env virtual environment.

01:27To really confirm that you’re in the virtual environment, use the pip list command again. This time, the list it produces is quite short — in fact, there’s only one package: pip itself!

01:41Create a new notebook that uses a library that will only exist in the my_env environment and not in your system’s main Python environment.

01:49Launch JupyterLab by entering jupyter lab on the command line and create a new notebook.

01:59In a new code cell, run the following:

! pip install vader-sentiment

02:02This will install VADER Sentiment Analysis, a library that provides a function for analyzing sentiment in social media posts. It is highly unlikely that it’s installed in your system’s main Python environment.

02:14Note: If you were wondering, VADER is short for “Valence Aware Dictionary and sEntiment Reasoner.”

02:22Because you installed VADER while the my_env environment was active, it’s installed in the my_env environment and available while that environment is active. It’s not installed in your system’s main Python environment.

02:37Play around with VADER a little. Enter the following into a code cell and run it:

from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

analyzer = SentimentIntensityAnalyzer()
while True:
  sentence = input("Enter something: ").strip()
  if not sentence:
    break
  analysis = analyzer.polarity_scores(sentence)
  print(analysis)

02:46Note: You may run into a ModuleNoFoundError on Mac. Running the following may remedy the issue:

! pip install --upgrade vader-sentiment

02:55The code above prompts the user to enter a sentence and returns VADER’s sentiment analysis of that sentence. VADER’s output is a dictionary containing ratings for negative, neutral, and positive sentiment, followed by a compound value derived from those ratings.

03:09Try entering sentences like these:

  • 03:12I rather liked the movie.
  • 03:16I really liked the movie.
  • 03:16I *really* liked the movie!
  • 03:20The movie had its moments, but dragged on.
  • 03:22I didn’t like the movie.
  • 03:12I hated the movie!
  • 03:25I REALLY hated that horrible movie!
  • 00:01👿!

03:29Save the notebook under the name vader.ipynb. Then, go to the command line and stop JupyterLab by typing control-C.

03:44When the prompt reappears, deactivate the virtual environment by entering the following:

deactivate

03:51You’ll be back in your system’s main Python environment. Your command line prompts will no longer be preceded by the virtual environment’s name.

03:58While in the same directory but no longer in the virtual environment, relaunch JupyterLab, open vader.ipynb and try running the code cell that asks for input again. This time, you should get an error message like this:

ModuleNotFoundError: No module named 'vaderSentiment'

04:13This makes sense. VADER is installed only in the my_env environment, not this one.